package com.general.client.widgets.button;

import com.general.client.action.ActionHandler;
import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.dom.client.MouseUpEvent;
import com.google.gwt.event.dom.client.MouseUpHandler;
import com.google.gwt.event.shared.HandlerRegistration;

/**
 * This class is used to show a button. to create a button with different style,
 * simply inherit the class and overwrite the getUiBinder method.
 */
public class Button extends AbstractButton implements IButton {

	@Override
	protected void init() {
		setUiBinder(getUiBinder());
		initWidget(uiBinder.createAndBindUi(this));
		mainPanel.addMouseDownHandler(new MouseDownHandler() {

			@Override
			public void onMouseDown(MouseDownEvent event) {
				if (isEnabled)
					mainPanel.removeStyleName(common.style().inactive());
			}
		});
		mainPanel.addMouseUpHandler(new MouseUpHandler() {

			@Override
			public void onMouseUp(MouseUpEvent event) {
				if (isEnabled)
					mainPanel.addStyleName(common.style().inactive());
			}
		});

		mainPanel.addKeyDownHandler(new KeyDownHandler() {

			@Override
			public void onKeyDown(KeyDownEvent event) {
				if (isEnabled) {
					if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER)
						mainPanel.removeStyleName(common.style().inactive());
				}
			}
		});

		mainPanel.addKeyUpHandler(new KeyUpHandler() {

			@Override
			public void onKeyUp(KeyUpEvent event) {
				if (isEnabled) {
					if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER)
						mainPanel.addStyleName(common.style().inactive());
				}
			}
		});

		mainPanel.addBlurHandler(new BlurHandler() {

			@Override
			public void onBlur(BlurEvent event) {
				mainPanel.addStyleName(common.style().inactive());
			}
		});

		mainPanel.addStyleName(common.style().inactive());
	}

	@Override
	public HandlerRegistration addActionHandler(final ActionHandler handler) {

		final HandlerRegistration clickHandler = mainPanel
				.addClickHandler(new ClickHandler() {

					@Override
					public void onClick(ClickEvent event) {
						if (isEnabled)
							handler.onAction(event);
					}
				});
		final HandlerRegistration keyHandler = mainPanel
				.addKeyDownHandler(new KeyDownHandler() {

					@Override
					public void onKeyDown(KeyDownEvent event) {
						if (isEnabled) {
							if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER)
								handler.onAction(event);
						}
					}
				});

		return new HandlerRegistration() {
			@Override
			public void removeHandler() {
				clickHandler.removeHandler();
				keyHandler.removeHandler();
			}
		};

	}

}